#ifndef INTERSECTION_CURVE_OF_SURFACES_H_
#define INTERSECTION_CURVE_OF_SURFACES_H_

#include<intersection_of_two_triangles.h>
#include<Mesh_IO/Mesh_IO.h>
#define quote lib_cell_quote
#ifdef __cplusplus
extern "C" {
#endif
//#include<tools_node.h>
typedef struct Crossover_Point {
    template_c* cs[2];

    
    double coordinates[2][3];

    
    template_v* vs[2];
    template_f* fs[2];

    void* prop;

}Crossover_Point;
static inline void crossover_point_init(Crossover_Point* cp)
{
    cp->cs[0]=NULL;cp->cs[1]=NULL;
    memset(cp->coordinates[0],0,sizeof(double)*3);
    memset(cp->coordinates[1],0,sizeof(double)*3);

    cp->vs[0]=NULL;cp->vs[1]=NULL;
    cp->fs[0]=NULL;cp->fs[1]=NULL;
    
    cp->prop=NULL;

}
static inline void crossover_point_free(Crossover_Point* cp)
{
    free(cp);
}

static inline double** get_intersection_from_two_cells(template_c* c1,template_c* c2)
{
    int len1=c1->vertices_size,len2=c2->vertices_size;
    double**poly1=(double**)malloc(sizeof(double*)*len1);
    double**poly2=(double**)malloc(sizeof(double*)*len2);

    for(int i=0;i<len1;i++)
    {
        poly1[i]=c1->vertices[i]->point;

    }
    for(int i=0;i<len2;i++)
    {
        poly2[i]=c2->vertices[i]->point;
    }
    double **re=get_intersection_points_from_two_polygons(poly1,len1,poly2,len2);
    free(poly1);free(poly2); 
    // int num=10251;
   
    // if(c1->id==num||c2->id==num)
    // {
        // for(int i=0;i<3;i++)
        // {
        //     printf("%.8lf %.8lf %.8lf\n",c1->vertices[i]->point[0],c1->vertices[i]->point[1],c1->vertices[i]->point[2] );
        // }
        // if(re!=NULL)
        // {
        //     printf("shi %lf %lf %lf %lf %lf %lf\n",re[0][0],re[0][1],re[0][2],re[1][0],re[1][1],re[1][2]);
           
        // }
        // else
        // {
        //     printf("null\n");
        // }
    // }
    return re;
}
// typedef struct Crossover_Point1{
    


// }Crossover_Point1;

//template_v** get_intersection_vertices_from_two_cells(template_c* c1,template_c* c2);
//make sure prop not be occupied
Node* simplify_node_of_two_nodes(Node* n1,Node *n2,int* flag);
//Node* get_intersection_lines_segments_of_two_nodes(Node* n1,Node* n2,Mesh* m);

void get_intersection_lines_of_two_nodes(Node* n1,Node* n2,Mesh*m);
void get_intersection_lines_of_two_nodesn(Node* n11,Node* n22,Mesh*m);

static void get_intersection_lines_of_two_meshs(Mesh* m1,Mesh* m2,Mesh*m)
{
     Node* n1=NULL,*n2=NULL;
    //  int test_num=86013;
    // if(m1->num_c(m1)>test_num)
    // {
    //     template_c c=m1->get_cell(m1,test_num); 
    //     for(int i=0;i<3;i++)
    //     {
    //         printf("c id:%d %lf %lf %lf \n",c.id,c.vertices[i]->point[0],c.vertices[i]->point[1],c.vertices[i]->point[2]);
    //     }
    // }
    // if(m2->num_c(m2)>test_num)
    // {
    //     template_c c=m1->get_cell(m2,test_num); 
    //     for(int i=0;i<3;i++)
    //     {
    //         printf("c id:%d %lf %lf %lf \n",c.id,c.vertices[i]->point[0],c.vertices[i]->point[1],c.vertices[i]->point[2]);
    //     }
    // }
    for(auto it=m1->c_begin(m1);it!=m1->c_end(m1);it++)
    {
        n1=node_overlying(n1,quote(it));
    } 
    for(auto it=m2->c_begin(m2);it!=m2->c_end(m2);it++)
    {
        n2=node_overlying(n2,quote(it));
    }
    get_intersection_lines_of_two_nodesn(n1,n2,m);
    free_node(n1);free_node(n2);
  //  interset_jiance(m);
   // printf("end here\n"); 
}
#ifdef __cplusplus
}
#endif

#endif